package com.hnkywl.sts.service.jhgl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.jhgl.JkdDao;
import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.entity.htgl.Cght;
import com.hnkywl.sts.entity.jhgl.Jkd;
import com.hnkywl.sts.entity.organ.Nbbm;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.service.organ.KhhService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-2
 * Time: 下午2:42
 * To change this template use File | Settings | File Templates.
 */
@Service
public class JkdService {
    private BmService bmService;
    private JkdDao jkdDao;
    private YgService ygService;
    private GsjgDao gsjgDao;
    private KhhService khhService;

    @Transactional
    public String findAllJkd(Long jkgsId, String jkgsMc, String gysMc, String jkdbm, int pageNo, int pageSize) {
        Long nbgsIds = null;
        if (jkgsId != null && jkgsMc.equals(gsjgDao.get(jkgsId).getJc())) {
            nbgsIds = nbgsIds;
        }
        Page<Jkd> JkdPage = this.jkdDao.findJkdList(nbgsIds, jkgsMc, gysMc, jkdbm, pageNo, pageSize);
        List<Jkd> JkdList = JkdPage.getResult() == null ? new ArrayList<Jkd>() : JkdPage.getResult();
        String JkdJson = BaseUtils.fetchCollectionToJson(JkdList, "id", "sbbm.jc", "zfsj", "sbgs.jc", "createUser.xm", "zt", "dateCreated", "jkdbm", "jkgs.jc", "gys.id", "gys.jc", "zfkx", "zffs.id", "zffs.z", "zfje", "yfje", "bcfk", "processInstanceId");
        return "{\"total\":" + JkdPage.getTotalCount() + ",\"rows\":[" + JkdJson + "]}";
    }

    @Transactional
    public String findytjjkdList(Long gysId, int pageNo, int pageSize) {
        Page<Jkd> JkdPage = this.jkdDao.findytjjkdList(gysId, pageNo, pageSize);
        List<Jkd> JkdList = JkdPage.getResult() == null ? new ArrayList<Jkd>() : JkdPage.getResult();
        String JkdJson = BaseUtils.fetchCollectionToJson(JkdList, "id", "zfsj", "sbgs.qc", "createUser.xm", "zt","cght.htbm","dateCreated", "jkdbm", "jkgs.qc", "gys.id", "gys.qc", "zfkx", "zffs.id", "zffs.z", "zfje", "yfje", "bcfk", "processInstanceId");
        return "{\"total\":" + JkdPage.getTotalCount() + ",\"rows\":[" + JkdJson + "]}";
    }


    @Transactional
    public String findJkdByZjjh(Long jkdId, String type) {
        List<Jkd> list = null;
        if ("zgzjjh".equalsIgnoreCase(type))
            list = jkdDao.findJkdByZgzjjh(jkdId);
        if ("tpzjjh".equalsIgnoreCase(type))
            list = jkdDao.findJkdByTpzjjh(jkdId);
        String json = BaseUtils.fetchCollectionToJson(list, "id", "zfsj", "jkdbm", "gys.qc", "zfkx", "zffs.z", "zfje", "yfje", "bcfk", "jkgs.qc", "sbgs.qc", "jkjg.qc", "processInstanceId", "createUser.xm", "zt", "dateCreated");
        return "{\"rows\":[" + json + "]}";
    }

    @Transactional
    public Jkd findJkdDetailByProcessInstance(String processInstanceId) {
        return this.jkdDao.findUniqueByProperty("processInstanceId", processInstanceId);
    }

    @Transactional
    public List<Jkd> findJkdDetailByCght(Long CghtId) {
        return this.jkdDao.findJkdDetailByCght(CghtId);
    }

    @Transactional
    public void changeJkdZt(Jkd jkd) {
        jkd.setLastUpdated(new Date());
    }

    @Transactional
    public Jkd findJkdDetail(Long jkdId) {
        return jkdDao.get(jkdId);
    }

    @Transactional
    public String findJkdDetailByJkdId(Long jkdId) {
        Jkd jkd = jkdDao.get(jkdId);
        Gson gson = new GsonBuilder().create();
        String json = gson.toJson(jkd);
        return "{\"rows\":" + json + "}";
    }

    @Transactional
    public void updateJkd(Jkd jkd) {
        Cght ht = jkd.getCght();
        Jkd clone = this.findJkdDetail(jkd.getId());
        clone = (Jkd) ReflectionUtils.getInstance(clone, jkd);
        clone.setLastUpdated(new Date());
        clone.setCght(ht);
        this.jkdDao.save(clone);
    }

    @Transactional
    public Jkd findJkdByJkdBh(String jkdbh) {
        return this.jkdDao.findJkdByJkdBh(jkdbh);
    }


    @Transactional
    public void saveJkd(Jkd jkd) {
        if (jkd.getZgzjjh() != null && jkd.getZgzjjh().getId() == null) {
            jkd.setZgzjjh(null);
        }
        if (jkd.getTpzjjh() != null && jkd.getTpzjjh().getId() == null) {
            jkd.setTpzjjh(null);
        }
        if (jkd.getCght() != null && jkd.getCght().getId() == null) {
            jkd.setCght(null);
        }
        if (jkd.getJkgs() != null && jkd.getJkgs().getId() == null) {
            jkd.setJkgs(null);
        }
        if (jkd.getJkjg() != null && jkd.getJkjg().getId() == null) {
            jkd.setJkjg(null);
        }
        if (jkd.getSqr() != null && jkd.getSqr().getId() == null) {
            jkd.setSqr(null);
        }
        if (jkd.getGys() != null && jkd.getGys().getId() == null) {
            jkd.setGys(null);
        }
        if (jkd.getZffs() != null && jkd.getZffs().getId() == null) {
            jkd.setZffs(null);
        }
        jkd.setDateCreated(new Date());
        jkd.setLastUpdated(jkd.getDateCreated());
        jkd.setCreateUser(YgService.getCurrentYg());
        jkd.setCreateOrgan(YgService.getCurrentYg().getZggs());
        jkd.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        jkd.setJkdbm(this.bmService.getJkdbm());
        Nbgs sbgs = new Nbgs();
        sbgs.setId(YgService.getCurrentYgZgGsId());
        Nbbm sbbm = new Nbbm();
        sbbm.setId(YgService.getCurrentYgGsId());
        jkd.setSbgs(sbgs);
        jkd.setSbbm(sbbm);
        this.jkdDao.save(jkd);
    }

    @Transactional
    public void saveJkdSyzt(Jkd jkd) {
        jkd.setLastUpdated(new Date());
        this.jkdDao.save(jkd);
    }

    //删除
    @Transactional
    public void deleteJkd(Long jkdId) {
        Jkd jkd = jkdDao.get(jkdId);
        if (jkd.getYfje().intValue() > 0) {
            throw new RuntimeException("此借款单已经付款，无法删除！");
        }
        jkd.setLastUpdated(new Date());
        jkd.setDeleted(1);
        this.jkdDao.save(jkd);
    }

    /**
     * 首页电子借款单
     *
     * @return
     */
    public String findAllDzjkd() {
        Integer dzjkdNum = 0;
        try {
            Date aDate = new Date(System.currentTimeMillis());
            Calendar calendar = GregorianCalendar.getInstance();
            calendar.setTime(aDate);
            return  jkdDao.findDzjkdSum( "ybc");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dzjkdNum.toString();
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }

    @Autowired
    public void setJkdDao(JkdDao jkdDao) {
        this.jkdDao = jkdDao;
    }

    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }

    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }

    @Autowired
    public void setKhhService(KhhService khhService) {
        this.khhService = khhService;
    }

}
